import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**世界boss属性枚举*/
export const enum E_WorldBossSnapshot {
	/**历史上打世界boss参与排行过*/
	RANK_JOINED = 0,
	/**历史最高名次 0-没用参与过，>= 1名次*/
	MAX_RANK_INDEX = 1,
	/**枚举数量*/
	EnumCount = 2
}


/**世界boss*/
export class WorldBossSnapshot {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_WORLD_BOSS_SNAPSHOT_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_WORLD_BOSS_SNAPSHOT_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: WorldBossSnapshot[] = [];

	/**创建入口*/
	public static create(json?: string): WorldBossSnapshot {
		const c = this.POOL.pop() || new WorldBossSnapshot();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**历史上打世界boss参与排行过*/
	protected _rankJoined: boolean;
	/**历史最高名次 0-没用参与过，>= 1名次 (uint32)*/
	protected _maxRankIndex: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (WorldBossSnapshot.POOL.indexOf(this) === -1) {
			WorldBossSnapshot.POOL.push(this);
		}
	}

	/**历史上打世界boss参与排行过*/
	public get rankJoined() {
		return this._rankJoined;
	}


	/**历史上打世界boss参与排行过*/
	public set rankJoined(v: boolean) {
		this._rankJoined = v;
	}


	/**历史最高名次 0-没用参与过，>= 1名次*/
	public get maxRankIndex() {
		return this._maxRankIndex;
	}


	/**历史最高名次 0-没用参与过，>= 1名次*/
	public set maxRankIndex(v: number) {
		this._maxRankIndex = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_WorldBossSnapshot): any {
		switch (k) {
			case E_WorldBossSnapshot.RANK_JOINED: return this._rankJoined;
			case E_WorldBossSnapshot.MAX_RANK_INDEX: return this._maxRankIndex;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_WorldBossSnapshot, v: any): void {
		switch (k) {
			case E_WorldBossSnapshot.RANK_JOINED: this._rankJoined = v; break;
			case E_WorldBossSnapshot.MAX_RANK_INDEX: this._maxRankIndex = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//历史上打世界boss参与排行过
		this._rankJoined = false;
		//历史最高名次 0-没用参与过，>= 1名次
		this._maxRankIndex = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._rankJoined) {
			json.rankJoined = this._rankJoined;
		}
		if (this._maxRankIndex) {
			json.maxRankIndex = this._maxRankIndex;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.rankJoined) {
			this._rankJoined = json.rankJoined;
		}
		if (json.maxRankIndex) {
			this._maxRankIndex = json.maxRankIndex;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeBool(this._rankJoined);
		byte.writeUint32(this._maxRankIndex);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._rankJoined = byte.readBool();
		this._maxRankIndex = byte.readUint32();
		return this;
	}


	/**克隆*/
	public clone(): WorldBossSnapshot {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = WorldBossSnapshot.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = WorldBossSnapshot.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//历史上打世界boss参与排行过
			if (idx === E_WorldBossSnapshot.RANK_JOINED) {
				const oldv = this._rankJoined;
				this._rankJoined = us[i + 1];
				IAsync.dispatch(eventID, idx, this._rankJoined, oldv);
				i += 2;
				continue;
			}
			//历史最高名次 0-没用参与过，>= 1名次
			if (idx === E_WorldBossSnapshot.MAX_RANK_INDEX) {
				const oldv = this._maxRankIndex;
				this._maxRankIndex = us[i + 1];
				IAsync.dispatch(eventID, idx, this._maxRankIndex, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in WorldBossSnapshot with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: WorldBossSnapshot): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._rankJoined !== other._rankJoined) {
			updates.push(E_WorldBossSnapshot.RANK_JOINED, this._rankJoined);
		}
		if (this._maxRankIndex !== other._maxRankIndex) {
			updates.push(E_WorldBossSnapshot.MAX_RANK_INDEX, this._maxRankIndex);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(WorldBossSnapshot.TOTAL_EVENT_ID, this.getV(0));
	}

}
